package com.xigua.xcar.setting;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.xigua.xcar.R;
import com.xigua.xcar.base.BaseActivity;
import com.xigua.xcar.constant.Constants;
import com.xigua.xcar.net.AbstractDataManager;
import com.xigua.xcar.net.update.UpdateData;
import com.xigua.xcar.net.update.UpdateDataManager;

public class AboutActivity extends BaseActivity {
	private static final String tag = "AboutActivity";
	private Button mBtnCheckUpdate = null;
	private ProgressDialog mProgressDialog = null;
	private boolean isGettingData = false;
	private UpdateDataManager mDM = null;
	
	@Override
	protected void finalize() throws Throwable {
		// 注销的注意事项
		super.finalize();
		if(null != mDM){
			mDM.setHanlder(null);
			mDM.cancelRequest();
			mDM = null;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setView(R.layout.about);
		setTopBarTitle("关于");
		setTitleBar(R.drawable.title_button, "返回", 0, "");
		mDM = new UpdateDataManager(this);
		mDM.setHanlder(new Handler(){
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				hideProgressDlg();
				isGettingData = false;
				switch(msg.what){
				case AbstractDataManager.RESULTCODE_SUCCESS:{
					UpdateData data = (UpdateData)msg.obj;
					if(null != data){
						if(null != data.getmUrl() && data.ismIsNeedUpdate()){
							showUpdateDlg(data.getmUrl(),data.getmUpdateDesc());
						}else{
							Toast.makeText(AboutActivity.this, null==data.getmUpdateDesc()?("亲，您好棒哦，是最新版本哦~~"):(data.getmUpdateDesc()), 3000).show();
						}
					}else{
						Toast.makeText(AboutActivity.this, "亲，风筝断线了", 3000).show();
					}
					break;
				}
				case AbstractDataManager.RESULTCODE_NETERR:
				case AbstractDataManager.RESULTCODE_OTHERERR:{
					Toast.makeText(AboutActivity.this, "亲，风筝断线了", 3000).show();
					break;
				}
				}
			}
		});
		mBtnCheckUpdate = (Button)findViewById(R.id.btn_about_update);
		mBtnCheckUpdate.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(isGettingData){
					return ;
				}
				showProgressDlg();
				sendRequest();
			}
		});
	}
	
	
	private void showUpdateDlg(String url,String content){
		final String sUrl = url;
		String sContent = null;
		if(null == content){
			content = "亲，有更多新功能哦...";
		}
		sContent = content;
		AlertDialog.Builder builder = new AlertDialog.Builder(this);  
	    builder.setTitle("更新提示");  
	    builder.setMessage(sContent);  
	    builder.setPositiveButton("更新",  
	            new DialogInterface.OnClickListener() {  
	                public void onClick(DialogInterface dialog, int whichButton) {           
                		Intent intent= new Intent();        
                        intent.setAction("android.intent.action.VIEW");    
                        Uri content_url = Uri.parse(sUrl);   
                        intent.setData(content_url);           
                        intent.setClassName("com.android.browser","com.android.browser.BrowserActivity");   
                        AboutActivity.this.startActivity(intent);
	                }  
	            });  
	    builder.setNegativeButton("取消",  
	            new DialogInterface.OnClickListener() {  
	                public void onClick(DialogInterface dialog, int whichButton) {  
	                }  
	            }).create(); 
	
	    builder.show();
	}
	
	/**
	 * 显示加载进度条
	 * */
	private void showProgressDlg() {
		if (mProgressDialog == null) {
			mProgressDialog = new ProgressDialog(this);
			// 设置样式
			mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			// 设置ProgressDialog 提示信息
			mProgressDialog.setMessage("加载中");
			// 设置ProgressDialog 的进度条是否不明确
			mProgressDialog.setIndeterminate(false);
			// 设置ProgressDialog 是否可以按退回按键取消
			mProgressDialog.setCancelable(true);
			// 让ProgressDialog显示
			mProgressDialog.show();
			mProgressDialog.setOnDismissListener(new OnDismissListener() {

				@Override
				public void onDismiss(DialogInterface dialog) {
					// TODO Auto-generated method stub
					Log.i(tag, "onDismiss");
					cancelRequest();
					mProgressDialog = null;
				}
			});
		}
	}
	
	private void hideProgressDlg(){
		if(null != mProgressDialog){
			mProgressDialog.dismiss();
			mProgressDialog = null;
		}
	}
	
	
	private void sendRequest() {
		if(null != mDM){
			if(!isGettingData){
				isGettingData = true;
				mDM.getDataFromServer();
			}
		}
	}
	
	private void cancelRequest() {
		isGettingData = false;
		mDM.cancelRequest();
	}
	
	@Override
	protected void handleTitleBarEvent(int buttonId) {
		switch (buttonId) {
		case Constants.LEFTBUTTONID: {
			finish();
			break;
		}
		case Constants.RIGHTBUTTONID: {
			break;
		}
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		cancelRequest();
		hideProgressDlg();
	}
	
	
}
